/**
 * 格式化聊天消息时间
 * @param date 日期对象或时间戳
 * @returns 格式化后的时间字符串
 */
export function formatChatTime(date: Date | string | number): string {
  const now = new Date();
  const targetDate = new Date(date);
  const pad2 = (n: number) => String(n).padStart(2, '0');
  const hhmm = `${pad2(targetDate.getHours())}:${pad2(targetDate.getMinutes())}`;
  
  // 检查是否是今天
  const isToday = now.toDateString() === targetDate.toDateString();
  
  // 检查是否是昨天
  const yesterday = new Date(now);
  yesterday.setDate(yesterday.getDate() - 1);
  const isYesterday = yesterday.toDateString() === targetDate.toDateString();
  
  // 检查是否是本周
  const weekAgo = new Date(now);
  weekAgo.setDate(weekAgo.getDate() - 7);
  const isThisWeek = targetDate > weekAgo;
  
  if (isToday) {
    // 今天：显示时间（24小时制）
    return hhmm;
  } else if (isYesterday) {
    // 昨天：显示"昨天 + 时间"
    return `昨天 ${hhmm}`;
  } else if (isThisWeek) {
    // 本周：显示星期几 + 时间
    const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    const weekday = weekdays[targetDate.getDay()];
    return `${weekday} ${hhmm}`;
  } else {
    // 更早：显示完整日期 + 时间
    const pad2 = (n: number) => String(n).padStart(2, '0');
    const mmdd = `${pad2(targetDate.getMonth() + 1)}-${pad2(targetDate.getDate())}`;
    return `${mmdd} ${hhmm}`;
  }
}

/**
 * 格式化时间（仅显示时间，24小时制）
 * @param date 日期对象或时间戳
 * @returns 格式化后的时间字符串
 */
export function formatTimeOnly(date: Date | string | number): string {
  const targetDate = new Date(date);
  const pad2 = (n: number) => String(n).padStart(2, '0');
  return `${pad2(targetDate.getHours())}:${pad2(targetDate.getMinutes())}`;
}

/**
 * 格式化日期（仅显示日期）
 * @param date 日期对象或时间戳
 * @returns 格式化后的日期字符串
 */
export function formatDateOnly(date: Date | string | number): string {
  const targetDate = new Date(date);
  const pad2 = (n: number) => String(n).padStart(2, '0');
  return `${targetDate.getFullYear()}-${pad2(targetDate.getMonth() + 1)}-${pad2(targetDate.getDate())}`;
}

/**
 * 获取相对时间描述
 * @param date 日期对象或时间戳
 * @returns 相对时间描述
 */
export function getRelativeTime(date: Date | string | number): string {
  const now = new Date();
  const targetDate = new Date(date);
  const diffMs = now.getTime() - targetDate.getTime();
  const diffMinutes = Math.floor(diffMs / (1000 * 60));
  const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
  const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
  
  if (diffMinutes < 1) {
    return '刚刚';
  } else if (diffMinutes < 60) {
    return `${diffMinutes}分钟前`;
  } else if (diffHours < 24) {
    return `${diffHours}小时前`;
  } else if (diffDays < 7) {
    return `${diffDays}天前`;
  } else {
    return formatDateOnly(date);
  }
}

/**
 * 格式化聊天列表时间
 * 专门用于聊天列表页面的时间显示，格式更简洁
 * @param date 日期对象或时间戳
 * @returns 格式化后的时间字符串
 */
export function formatChatListTime(date: Date | string | number): string {
  const now = new Date();
  const targetDate = new Date(date);
  const pad2 = (n: number) => String(n).padStart(2, '0');
  const hhmm = `${pad2(targetDate.getHours())}:${pad2(targetDate.getMinutes())}`;
  
  // 检查是否是今天
  const isToday = now.toDateString() === targetDate.toDateString();
  
  // 检查是否是昨天
  const yesterday = new Date(now);
  yesterday.setDate(yesterday.getDate() - 1);
  const isYesterday = yesterday.toDateString() === targetDate.toDateString();
  
  // 检查是否是本周
  const weekAgo = new Date(now);
  weekAgo.setDate(weekAgo.getDate() - 7);
  const isThisWeek = targetDate > weekAgo;
  
  if (isToday) {
    // 今天：只显示时间（24小时制）
    return hhmm;
  } else if (isYesterday) {
    // 昨天：显示"昨天"
    return '昨天';
  } else if (isThisWeek) {
    // 本周：显示星期几
    const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    return weekdays[targetDate.getDay()];
  } else {
    // 更早：显示月/日
    return `${pad2(targetDate.getMonth() + 1)}-${pad2(targetDate.getDate())}`;
  }
}
